

var urlRoot = "http://47.104.102.188:7979";

$(function(){
    var config = getConfig();
    urlRoot = config["data"]["url_root"];
    console.log("data-url: " + urlRoot);

    getDatas();

    addDataSubmit(); // 添加数据提交处理

});

document.oncontextmenu = function(){
    return false;
}

function getDatas() {
    var test = false;
    if(test)
    {
        var url = urlRoot + "/bio-analysis-web/datas/my_datas.json";
        console.log("project url: " + url);
        $.get(url, function(data) {
            //var dataJson = JSON.parse(data);
            //console.log(projData);
        });

        var dataJson = {
            "datas": [
                {
                    "data_name": "data01",
                    "data_path": "/my/样本数据1",
                    "data_desc": "样本数据1，样本数据1，样本数据1，样本数据1，",
                    "state": true
                },
                {
                    "data_name": "data02",
                    "data_path": "/my/样本数据2",
                    "data_desc": "样本数据2，样本数据2，样本数据2，样本数据2，",
                    "state": true
                },
                {
                    "data_name": "data03",
                    "data_path": "/my/样本数据3",
                    "data_desc": "样本数据3，样本数据3，样本数据3，样本数据3，",
                    "state": true
                }
            ]
        }

        fillTable(dataJson.datas);
    }
    else
    {
        refreshData();
    }
}


function refreshData()
{
    var user = getUserID();

    $.ajax({
        type: "POST",
        url: urlRoot + "/api/user/data/list",
        data: {login_id : user},
        dataType: "json",
        beforeSend: setRequestHeader,
        success: function(data){
            //alert(JSON.stringify(data));
            if(data.success)
            {
                fillTable(data.data);
            }
            else
            {
                MessageBox("show", "发生错误", data.message);
            }

        },
        error : function (err) {

        }
    });
}

var dataItemList = [];

function fillTable(data)
{
    $('#data_table thead').siblings().remove();

    console.log(data);
    for(var i=0; i<data.length; ++i)
    {
        var it = data[i];

        var item = document.createElement('tr');
        item.setAttribute("id", it.id);
        item.dataID = it.id;
        item.dataName = it.data_name;
        item.innerHTML = `<td>${it.data_name}</td>
            <td>${it.data_path}</td>
            <td>${it.data_desc}</td>
            <td>${it.state}</td>
            <td><input type="button" class="del_btn" value="X" onmouseup="delItem(this)" /></td>`;
        $('#data_table').append(item);

        dataItemList.push(item);

        /*
        var id = it.data_name + "222";
        var btn = document.createElement("input");
        btn.setAttribute("type", "button");
        btn.setAttribute("value", "X");
        btn.setAttribute("id", id);
        btn.setAttribute("class", "del_btn");
        btn.setAttribute("onmousedown", "delItem(id)");
        $('#del'+it.data_name).append(btn);
         */

    }
}

function searchData()
{
    var text = $("#searchInput").val().trim();
    if(text == "") {
        for(var i=0; i<dataItemList.length; ++i) {
            dataItemList[i].css("display","block");
        }
    } else {
        for(var i=0; i<dataItemList.length; ++i)
        {
            var item = dataItemList[i];
            if(item.id.indexOf(text) != -1)
            {
                item.hidden = false;
                //item.css("display","block");
            } else {
                item.hidden = true;
                //item.css("display","none");
            }
        }
    }
}

function searchDataChange()
{
    var text = $("#searchInput").val().trim();
    if(text == "") {
        for(var i=0; i<dataItemList.length; ++i) {
            dataItemList[i].hidden = false;
            //projItemList[i].css("display","block");
        }
    }
}

function navGo(toAdd)
{
    var my = $("#mydata");
    if(my[0] != null)
        my[0].hidden = toAdd;

    var add = $("#adddata");
    if(add[0] != null)
        add[0].hidden = !toAdd;
}

function addDataSubmit()
{
    // 提交数据
    $('#add_form').bind('submit', function(){
        add_data_submit(this, function(data){
            if(data.success)
            {
                MessageBox("show", "操作成功", "添加数据完成！");
                addDatHide();
                refreshData();
            }
            else
            {
                MessageBox("show", "发生错误", data.message);
            }
        });
        return false;
    });
}

//将form转为AJAX提交
function add_data_submit(frm, fn) {
    var dataPara = getFormJson(frm);

    var user = getUserID(); //window.localStorage.getItem("user_id");

    $.ajax({
        type: "POST",
        url: urlRoot + "/api/user/data/add",
        //xhrFields:{withCredentials:true},
        data: {
            login_id : user,
            data_name : dataPara.data_name,
            data_path :  dataPara.data_path,
            data_desc :  dataPara.data_desc
        },
        dataType: "json",
        beforeSend: setRequestHeader,
        success: fn,
        error : function (err) {

        }
    });
}


// 发送上传方法
function sendToolMethon()
{
    $.ajax({
        type: "POST",
        url: urlRoot + "/api/instruction/upload_file",
        data: null, //{username:$("#username").val(), content:$("#content").val()},
        dataType: "json",
        beforeSend: setRequestHeader,
        success: function(res){
            if(res.success)
                MessageBox("show", "操作成功", res.message);
            else
                MessageBox("show", "发生错误", res.message);
        },
        error : function (err) {

        }
    });
}

function  addDataShow() {
    // 显示遮罩和对话框
    $('.add-data-wrap-dialog').css("display", "flex");
}

function  addDatHide()
{
    //$('.add-data-wrap-dialog').addClass("hide");
    $('.add-data-wrap-dialog').css("display", "none");
}

// 删除对话框 -----------------
var delDataID;
function delItem(btn) {
    delDataID = btn.parentNode.parentNode.dataID;
    var delDataName = btn.parentNode.parentNode.dataName;
    var message = "你确认删除数据："+ delDataName + " 吗？";

    $('.dialog-message').html(message);
    // 显示遮罩和对话框
    $('.wrap-dialog').removeClass("hide");
}

function delOk()
{
    $('.wrap-dialog').addClass("hide");
    if(delDataID)
    {
        $.ajax({
            type: "POST",
            url: urlRoot + "/api/user/data/del",
            data: { login_id: getUserID(), data_id: delDataID},
            dataType: "json",
            beforeSend: setRequestHeader,
            success: function(res){
                if(res.success)
                {
                    //MessageBox("show", "操作成功", res.message);
                    refreshData();
                }
                else
                    MessageBox("show", "发生错误", res.message);
            },
            error : function (err) {

            }
        });
    }
    delDataID = null;
}

function delCancel() {
    $('.wrap-dialog').addClass("hide");
    if(delDataID)
    {
        console.log("cancel  " + delDataID);
    }
    delDataID = null;
}
